<?php

/**
 * 第三方注册登录记录
 *
 * @author camfee <camfee@foxmail.com>
 *
 * $Id$
 */

namespace Center;

use lib\core\Module;
use Common\Bridge;

class Connect extends Module
{
    // 列表缓存KEY
    const MC_USER_LIST = 'ConnectUser:{UserId}';

    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'Connect',
        'fields' => [
            'ConnectId' => self::VAR_TYPE_KEY,
            'UserId' => self::VAR_TYPE_INT,
            'SiteId' => self::VAR_TYPE_INT,
            'AccessToken' => self::VAR_TYPE_STRING,
            'RefreshToken' => self::VAR_TYPE_STRING,
            'OpenId' => self::VAR_TYPE_STRING,
            'UnionId' => self::VAR_TYPE_STRING,
            'NickName' => self::VAR_TYPE_STRING,
            'ExpiredAt' => self::VAR_TYPE_STRING,
            'UpdatedAt' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        'mckey' => 'Connect_%d',
        'mctime' => 0
    ];

    /**
     * 用户增加金币
     *
     * @param array $data 用户信息
     * @return bool
     */
    public static function addConnect($data)
    {
        if (empty($data['OpenId']) || empty($data['UserId']) || empty($data['SiteId'])) {
            return false;
        }
        $data['CreateTime'] = date('Y-m-d H:i:s');
        $ret = parent::addData($data);

        if ($ret > 0) {
            // 清除列表的缓存
            $mc = Bridge::memcache(self::$_conf['mc']);
            $mc->delete(str_replace('{UserId}', $data['UserId'], self::MC_USER_LIST));

            return true;
        }

        return false;
    }

    /**
     * 获取用户第三方登录记录
     *
     * @param string $uid 用户id
     * @return array
     */
    public static function getConnectByUserId($uid)
    {
        $where = [
            'UserId' => $uid,
        ];
        $extra = [
            'type' => parent::MOD_TYPE_MEMCACHE,
            'mckey' => self::MC_USER_LIST,
            'mtime' => 86400,
        ];
        $ret = parent::getDataByFields($where, $extra);
        return $ret;
    }

    /**
     * 获取用户记录
     *
     * @param string $openid 开放平台唯一ID
     * @param int $siteid 第三方站点ID
     * @return array
     */
    public static function getConnectByOpenId($openid, $siteid)
    {
        $where = [
            'OpenId' => $openid,
            'SiteId' => $siteid
        ];
        $extra = [
            'fields' => '*',
            'limit' => 1
        ];
        $ret = parent::getDataByFields($where, $extra);
        return !empty($ret['data']) ? current($ret['data']) : [];
    }

    /**
     * 获得一条或多条 用户记录 数据
     *
     * @param int|array $id ConnectId
     * @param array $extra 见 parent::EXTRA_*
     * @return array    单条返回['feild1'=>'value1',..], 多条返回['ConnectIdID' => [...],...]
     */
    public static function getConnectById($id, $extra = [])
    {
        return parent::getDataById($id, $extra);
    }

    /**
     * 更新第三方用户信息
     *
     * @param int $connectid 主键
     * @param array $info 修改的字段
     * @return bool
     */
    public static function updateConnect($connectid, $info)
    {
        return parent::updateData($connectid, $info);
    }
    /**
     * 删除第三方用户信息
     *
     * @param int $connectid 主键
     * @return bool
     */
    public static function delConnect($connectid)
    {
        return parent::delData($connectid);
    }
}